/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.statements;

import org.databasefacade.table.Table;
import org.databasefacade.table.column.AliasedColumnList;
import org.databasefacade.table.column.Column;

public class InsertStatement implements DatabaseStatement {
	private boolean isValuePairedInsert=false;
	private boolean isCleared=false;
	private Table insertTable;
	private AliasedColumnList columns;
	private Object[] values;
	private SelectStatement selectStmt;
	
	/**
	 * Will create and insert for all columns except
	 * the primary key if it is an auto-increment.
	 * 
	 * If the into is called then the into column list
	 * is cleared and only the fields selected by the 
	 * developer are inserted.
	 * 
	 * @param table
	 */
	public InsertStatement(Table table) {
		init(table, null, false);
		columns = new AliasedColumnList(table.getDBHelper().getTranslator());
	}
	
	/**
	 * Will create a insert based on the order of definition
	 * of the table columns using the select statement.
	 * 
	 * @param table
	 * @param stmt
	 */
	public InsertStatement(Table table, SelectStatement stmt) {
		init(table, stmt, false);
	}
	
	/**
	 * Will create a insert statement into columns defined
	 * by the select statement aliases if the developer uses
	 * the useSelectAliases=true
	 * 
	 * If the developer starts to define a list of insert 
	 * columns then the list is cleared and the developer 
	 * must define the entire list.
	 * 
	 * @param table
	 * @param stmt
	 */
	public InsertStatement(Table table, SelectStatement stmt, boolean useSelectAliases) {
		init(table, stmt, true);
	}
	
	
	
	
	/**
	 * Creates a parameter pairing with the column
	 * @param column
	 */
	public InsertStatement into(Column column) {
		if ( ! isCleared ) {
			columns.clearColumns();
		}
		if ( isValuePairedInsert ) {
			columns.clearColumns();
		}
		return this;
	}
	
	/**
	 * Creates a parameter pairing for all columns
	 * @param columns
	 */
	public InsertStatement into(AliasedColumnList columns) {
		if ( ! isCleared ) {
			columns.clearColumns();
		}
		if ( isValuePairedInsert ) {
			columns.clearColumns();
		}	
		return this;
	}
	
	/**
	 * Creates a column value pairing. Once the insert 
	 * starts with this then it must continue doing
	 * value pairings.
	 * 
	 * @param column
	 * @param value
	 */
	public InsertStatement into(Column column, Object value) {
		if ( ! isCleared ) {
			columns.clearColumns();
		}	
		if ( ! isValuePairedInsert ) {
			columns.clearColumns();
		}	
		return this;
	}
	
	/**
	 * Creates a column value pairing. Once the insert 
	 * starts with this then it must continue doing
	 * value pairings. The value in this case comes
	 * from a select statement.
	 * 
	 * @param column
	 * @param value
	 */
	public InsertStatement into(Column column, SelectStatement value) {
		if ( ! isCleared ) {
			columns.clearColumns();
		}	
		if ( ! isValuePairedInsert ) {
			columns.clearColumns();
		}	
		return this;
	}

	
	/**
	 * Return the generated SQL for this statement
	 */
	public String getSQL() {
		StringBuilder sb = new StringBuilder("");
		sb.append("INSERT INTO " + insertTable.getTableName());
		if ( values != null ) {
			
		} else if ( selectStmt != null ) {
			
		}
		return null;
	}
	
	private void init(Table table, SelectStatement stmt, boolean useSelectAliases) {
		insertTable=table;
		selectStmt = stmt;
	}
}
